package bt2array

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
	"gitee.com/ljfirst/algo-go-sdk/src/data_structure/heap_stack_queue/stack"
	"gitee.com/ljfirst/algo-go-sdk/src/data_structure/list"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/7/23 00:11
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 **/
type PreOrderByStack struct {
	ans []int
}

func (m *PreOrderByStack) Method(node *list.Node) []int {
	if node == nil {
		return nil
	}
	if len(m.ans) == 0 {
		m.ans = make([]int, 0)
	}
	stackTools := stack.ArrayStack{}
	stackTools.Push(node)
	for stackTools.Size() != 0 {
		nodeTemp := stackTools.Pop().(*list.Node)
		m.ans = append(m.ans, nodeTemp.Value.(int))
		if nodeTemp.Right != nil {
			stackTools.Push(nodeTemp.Right)
		}
		if nodeTemp.Left != nil {
			stackTools.Push(nodeTemp.Left)
		}
	}
	return m.ans
}

func (m *PreOrderByStack) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.Tree, C.PreOrder, C.Stack},
		Desc: &C.Desc{
			Name:        "PreOrderByStack",
			NameCn:      "先序遍历",
			Description: "使用栈进行先序遍历",
			ParamsDesc: map[string]string{
			},
			Example: map[int]string{
			},
		},
	}
}
